package design.patterns.mementomode.checkpointmemento;

import java.util.ArrayList;
import java.util.List;

/**
 * @Desc:   发起人角色
 * @author: cww
 * @DateTime: 2020-06-13 18:18
 */

public class Originator {
    private List<String> states;
    private int index;

    public Originator() {
        states = new ArrayList<>();
        index = 0;
    }

    /**
     * 工厂方法，返回一个备忘录对象
     * @return
     */
    public Memento createMemento(){
        return new Memento(states, index);
    }

    /**
     * 还原备忘录中的状态到发起人内部
     * @param memento
     */
    public void restoreMemento(Memento memento){
        this.states = memento.getStates();
        index = memento.getIndex();
    }

    /**
     * 对发起人状态的赋值
     * @param states
     */
    public void setStates(String states) {
        this.states.add(states);
        index++;
    }

    /**
     * 辅助方法
     */
    public void printState() {
        System.out.println("Total number of states: "+ index);
        for (String state : states) {
            System.out.println(state);
        }
    }
}
